package ltd.newbee.mall.service.impl;

import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.controller.vo.SearchPageCategoryVO;
import ltd.newbee.mall.dao.FilmMapper;
import ltd.newbee.mall.entity.Film;
import ltd.newbee.mall.entity.NewBeeMallGoods;
import ltd.newbee.mall.service.FilmService;
import ltd.newbee.mall.util.PageQueryUtil;
import ltd.newbee.mall.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FilmServicelmpl implements FilmService {

    @Autowired
    private FilmMapper filmMapper;

    @Override
    public PageResult getFilmsPage(PageQueryUtil pageUtil) {
        List<Film> filmList = filmMapper.findFilmsList(pageUtil);
        int total = filmMapper.getTotalFilm(pageUtil);
        PageResult pageResult = new PageResult(filmList, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public Boolean batchUpdateSellStatus(Long[] ids, int sellStatus) {
        return filmMapper.batchUpdateSellStatus(ids, sellStatus) > 0;
    }

    @Override
    public Film selectFilm(int filmId) {
        return filmMapper.selectByPrimaryKey(filmId);
    }

    //查询所有电影
    @Override
    public List<Film> getFilms() {
        return filmMapper.selectAll();
    }

    //添加电影
    @Override
    public String saveFilm(Film film) {
        if (filmMapper.insertSelective(film) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }
    //查询单个电影详情
    @Override
    public Film selectById(Long id) {
        return filmMapper.selectByPrimaryKey(Integer.parseInt(String.valueOf(id)));
    }

    @Override
    public String UpdateFilm(Film film) {
        Film temp = filmMapper.selectByPrimaryKey(film.getFilmId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        film.setFileReleaseTime(new Date());
        if (filmMapper.updateByPrimaryKeySelective(film) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

}
